import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/order/apply_invoice_viewmodel.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// @description: 申请开票
/// @author tangzhentao
/// @created at 13:26 2024/12/14

class ApplyInvoicePage extends StatelessWidget {
  /// 订单信息
  final int orderId;

  const ApplyInvoicePage({super.key, required this.orderId});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<ApplyInvoiceViewmodel>(
      appBar: (_) => xfgAppbar(title: '申请开票'),
      model: ApplyInvoiceViewmodel(orderId),
      builder: (ctx, invoice) => Padding(
        padding: EdgeInsets.all(XfgSize().size10),
        child: Column(
          children: [
            Container(
              decoration: white6Decoration,
              child: Column(
                children: [
                  _itemWidget('订单编号', XfgSize().size50, bottomBorder: false, child: XfgText('165449879461621233216165', fontSize: XfgSize().text16, color: XfgColor.text1)),
                  _itemWidget('开票金额', XfgSize().size50, bottomBorder: false, child: XfgText('￥', fontSize: XfgSize().text12, color: XfgColor.theme, children: [
                    TextSpan(text: '888', style: TextStyle(fontSize: XfgSize().text20, fontWeight: FontWeight.bold)),
                    TextSpan(text: '.62', style: TextStyle(fontSize: XfgSize().text12, fontWeight: FontWeight.bold)),
                  ]))
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: XfgSize().size10),
              decoration: white6Decoration,
              child: Column(
                children: [
                  _itemWidget('发票类型', XfgSize().size52, child: InkWell(
                    onTap: () {},
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        XfgText('增值税电子普通发票', fontSize: XfgSize().text14, color: XfgColor.text3),
                        SizedBox(width: XfgSize().size28),
                        Image.asset(Res.imagesArrowRight, width: XfgSize().size10, height: XfgSize().size15,)
                      ],
                    ),
                  )),
                  _itemWidget('抬头类型', XfgSize().size52, child: Builder(builder: (ctx) {
                    final personal = ctx.select<ApplyInvoiceViewmodel, bool>((k) => k.personal);
                    return InkWell(
                      onTap: () {},
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          _radioTextWidget('个人或事业单位', personal, onTap: () => invoice.personal = !personal),
                          SizedBox(width: XfgSize().size40),
                          _radioTextWidget('企业', !personal, onTap: () => invoice.personal = !personal),
                        ],
                      ),
                    );
                  })),
                  _itemWidget('发票抬头', XfgSize().size52, child: InkWell(
                    onTap: () {},
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        XfgText('填写需要开具发票的企业名称', fontSize: XfgSize().text14, color: XfgColor.text3),
                        SizedBox(width: XfgSize().size8),
                        Image.asset(Res.imagesArrowRight, width: XfgSize().size10, height: XfgSize().size15,)
                      ],
                    ),
                  )),
                  _itemWidget('税号', XfgSize().size52, child: XfgText('纳税人识别号', fontSize: XfgSize().text14, color: XfgColor.text3, textAlign: TextAlign.end)),
                  Container(
                    height: XfgSize().size52,
                    padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                    child: Row(
                      children: [
                        XfgText('设置为默认抬头', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold),
                        Expanded(child: Builder(builder: (ctx) {
                          final defaultInvoice = ctx.select<ApplyInvoiceViewmodel, bool>((k) => k.defaultInvoice);
                          return InkWell(
                            onTap: () => invoice.defaultInvoice = !defaultInvoice,
                            child: Container(
                              alignment: Alignment.centerRight,
                              child: xfgRadio(value: defaultInvoice, size: XfgSize().size18),
                            ),
                          );
                        })),
                      ],
                    ),
                  )
                ],
              ),
            ),
            const Spacer(),
            TextButton(
              onPressed: invoice.submitApplication,
              style: xfgButtonStyle(Size(XfgSize().size342, XfgSize().size50), radius: XfgSize().size50),
              child: XfgText('提交申请', fontSize: XfgSize().text20, color: XfgColor.white),
            ),
            SizedBox(height: XfgSize().size20 + ( XfgSize().bottomBarHeight == 0 ? XfgSize().size30:XfgSize().bottomBarHeight))
          ],
        ),
      )
    );
  }

  /// 通用布局
  Widget _itemWidget(String text, double height, {bool bottomBorder = true, required Widget child}) => Container(
    height: height + (bottomBorder ? XfgSize().size0_5:0),
    decoration: bottomBorder ? bottomBorderDecoration:null,
    padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
    child: Row(
      children: [
        SizedBox(
          width: XfgSize().size82,
          child: XfgText(text, fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold),
        ),
        Expanded(child: child),
      ],
    ),
  );

  /// raido + 字符串
  Widget _radioTextWidget(String text, bool select, {GestureTapCallback? onTap}) => InkWell(
    onTap: onTap,
    child: Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        xfgRadio(value: select, size: XfgSize().size18),
        SizedBox(width: XfgSize().size8),
        XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text3),
      ],
    ),
  );
}
